﻿using Bll;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using Utils;

namespace Vote.Controllers
{
    public class VoteApiController : Controller
    {
        #region redis相关
        /// <summary>
        /// 更新节目信息
        /// </summary>
        public JsonResult InsertVoteShowToRedis()
        {
            Result<bool> result= new Result<bool>();
            try
            {
                BllVoteNumber.InsertVoteShowToRedis();
                result.Status = 200;
                result.Body = true;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Body = false;
                result.Msg = "更新节目信息缓存出错";
                LogHelper.WriteErrorLog("更新节目信息缓存出错："+ex.Message);
            }

            return Json(result,JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新场次信息
        /// </summary>
        public JsonResult InsertVoteNumberToRedis()
        {
            Result<bool> result = new Result<bool>();
            try
            {
                BllVoteNumber.InsertVoteNumberToRedis();
                result.Status = 200;
                result.Body = true;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Body = false;
                result.Msg = "更新场次信息缓存出错";
                LogHelper.WriteErrorLog("更新场次信息缓存出错：" + ex.Message);
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新关注信息
        /// </summary>
        public JsonResult InsertSubcribeToRedis()
        {
            Result<bool> result = new Result<bool>();
            try
            {
                BllVoteNumber.InsertSubcribeToRedis();
                result.Status = 200;
                result.Body = true;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Body = false;
                result.Msg = "更新关注信息缓存出错";
                LogHelper.WriteErrorLog("更新关注信息缓存出错：" + ex.Message);
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult UpdateOneSubcribeToRedis(string openId,bool flag)
        {
            Result<bool> result = new Result<bool>();
            try
            {
                BllVoteNumber.UpdateOneSubcribeToRedis(openId,flag);
                result.Status = 200;
                result.Body = true;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Body = false;
                result.Msg = "更新单条关注信息缓存出错";
                LogHelper.WriteErrorLog("更新单条关注信息缓存出错：" + ex.Message);
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        public JsonResult InsertUserInfoToRedis()
        {
            Result<bool> result = new Result<bool>();
            try
            {
                BllVoteNumber.InsertUserInfoToRedis();
                result.Status = 200;
                result.Body = true;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Body = false;
                result.Msg = "更新用户信息信息缓存出错";
                LogHelper.WriteErrorLog("更新用户信息信息缓存出错：" + ex.Message);
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新节目视频信息
        /// </summary>
        public JsonResult InsertVideoUrlToRedis()
        {
            Result<bool> result = new Result<bool>();
            try
            {
                BllVoteNumber.InsertVideoUrlToRedis();
                result.Status = 200;
                result.Body = true;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Body = false;
                result.Msg = "更新节目视频缓存出错";
                LogHelper.WriteErrorLog("更新节目视频缓存出错：" + ex.Message);
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新所有缓存
        /// </summary>
        public JsonResult InsertAllToRedis()
        {
            Result<bool> result = new Result<bool>();
            try
            {
                BllVoteNumber.InsertVoteShowToRedis();
                BllVoteNumber.InsertVoteNumberToRedis();
                BllVoteNumber.InsertSubcribeToRedis();
                BllVoteNumber.InsertUserInfoToRedis();
                BllVoteNumber.InsertVideoUrlToRedis();
                result.Status = 200;
                result.Body = true;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Body = false;
                result.Msg = "更新所有缓存出错";
                LogHelper.WriteErrorLog("更新所有缓存出错：" + ex.Message);
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}